<?php
namespace jy_merch3fee;
defined('BY_JYA') or exit('error');
/*
	任务
*/
class task extends \plugin{
    public function jifen_sf($setting=array()){
		if($setting){
			$this->setting=$setting;
		}
        $daytime = mktime(0,0,0,date('m'),date('d'),date('Y'));
		$lasdaytime=$daytime-24*3600;
        $createtime=$daytime;
        $isplan = pdo_get('jy_merch3fee_feelog',array('uniacid'=>$_SESSION['uniacid'],'type'=>1,'createtime >'=>$createtime),array('id'));
        if($isplan){
           return false;
        }
		$merchs = pdo_getall('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'status'=>1),array('id'));
		$merchs[]=array('id'=>0);
		foreach($merchs as $mv){
			$allfee=0;
			$dayallnengfee = pdo_getsum('jy_merch3fee_order',array('merchid'=>$mv['id'],'uniacid'=>$_SESSION['uniacid'],'nengfee >'=>0,'createtime between'=>array($lasdaytime,$createtime)),'nengfee');
			if($this->setting['nengfee_daynum']>0){
				$dayallnengfee +=$this->setting['nengfee_daynum'];
			}
			$params=array('uniacid'=>$_SESSION['uniacid'],'mid >'=>0,'credit5 >'=>0);
			if($mv['id']){
				$setting=$this->plugin_setting('setting'.$mv['id']);
				if(!$setting['nengfee_open']){
					continue;
				}
				$params['merchid']=$mv['id'];
				$allmemberfee = pdo_getsum('jy_merch_member',$params,'credit5');
			}else{
				if(!$this->setting['nengfee_open']){
					continue;
				}
				$params=array('uniacid'=>$_SESSION['uniacid'],'credit5 >'=>0);
				$allmemberfee = pdo_getsum('core_members',$params,'credit5');
			}
			if($dayallnengfee>0 && $allmemberfee>0){
				$params=array('uniacid'=>$_SESSION['uniacid'],'mid >'=>0);
				$params['merchid']=$mv['id'];
				$members = pdo_getall('jy_merch3fee_member',$params,array('mid'));
				if($members){
					foreach($members as $item){
						if($mv['id']){
							$item['credit5']=pdo_getcolumn('jy_merch_member',array('merchid'=>$mv['id'],'uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'credit5');
						}else{
							$item['credit5']=pdo_getcolumn('core_members',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['mid']),'credit5');
						}
						if($item['credit5'] <=0){
							continue;
						}
						$onefee=$dayallnengfee/$allmemberfee;
						$fee=$onefee*$item['credit5'];
						if($fee > 0){
							$log=array(
								'mid'=>$item['mid'],
								'merchid'=>$mv['id'],
								'fee'=>$fee,
								'is_add'=>1,
							);
							$this->add_nlog($log,0);
							$this->update('member',array('nengfee +='=>$fee),array('mid'=>$item['mid'],'merchid'=>$mv['id']));
						}
						$allfee +=$fee;
					}
				}
			}
			$log=array(
				'merchid'=>$mv['id'],
				'allfee'=>$allfee,
				'allmemberfee'=>$allmemberfee,
				'dayallnengfee'=>$dayallnengfee,
			);
			$this->add_feelog($log,1);
		}
        return true;
    }
	public function add_feelog($log=array(),$type=0){
		$feelog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'merchid'=>$log['merchid'],
			'allfee'=>$log['allfee'],
			'dayallnengfee'=>$log['dayallnengfee'],
			'allmemberfee'=>$log['allmemberfee'],
			'type'=>$type,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('feelog',$feelog);
		return true;
	}
	public function add_nlog($log=array(),$type=0){
		$nlog=array(
			'uniacid'=>$_SESSION['uniacid'],
			'mid'=>$log['mid']?:$_SESSION['uid'],
			'merchid'=>$log['merchid'],
			'fee'=>$log['fee'],
			'is_add'=>$log['is_add'],
			'type'=>$type,
			'createtime'=>TIMESTAMP,
		);
		$this->insert('nlog',$nlog);
		return true;
	}
}